package com.dianping.cat.home.router.transform;

import com.dianping.cat.home.router.IEntity;
import com.dianping.cat.home.router.IVisitor;
import com.dianping.cat.home.router.entity.DefaultServer;
import com.dianping.cat.home.router.entity.Domain;
import com.dianping.cat.home.router.entity.Group;
import com.dianping.cat.home.router.entity.GroupServer;
import com.dianping.cat.home.router.entity.Network;
import com.dianping.cat.home.router.entity.NetworkPolicy;
import com.dianping.cat.home.router.entity.RouterConfig;
import com.dianping.cat.home.router.entity.Server;
import com.dianping.cat.home.router.entity.ServerGroup;
import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
























public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

/*  51 */   public DefaultXmlBuilder() { this(false); }



/*  55 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }

  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  59 */     this.m_compact = compact;
/*  60 */     this.m_sb = sb;
/*  61 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }

  public String buildXml(IEntity<?> entity) {
/*  65 */     entity.accept(this.m_visitor);
/*  66 */     return this.m_sb.toString();
  }

  protected void endTag(String name) {
/*  70 */     this.m_level--;

/*  72 */     indent();
/*  73 */     this.m_sb.append("</").append(name).append(">\r\n");
  }


/*  77 */   protected String escape(Object value) { return escape(value, false); }


  protected String escape(Object value, boolean text) {
/*  81 */     if (value == null) {
/*  82 */       return null;
    }

/*  85 */     String str = toString(value);
/*  86 */     int len = str.length();
/*  87 */     StringBuilder sb = new StringBuilder(len + 16);

/*  89 */     for (int i = 0; i < len; i++) {
/*  90 */       char ch = str.charAt(i);

/*  92 */       switch (ch) {
        case '<':
/*  94 */           sb.append("&lt;");
          break;
        case '>':
/*  97 */           sb.append("&gt;");
          break;
        case '&':
/* 100 */           sb.append("&amp;");
          break;
        case '"':
/* 103 */           if (!text) {
/* 104 */             sb.append("&quot;");
            break;
          }
        default:
/* 108 */           sb.append(ch);
          break;
      }

    }
/* 113 */     return sb.toString();
  }

  protected void indent() {
/* 117 */     if (!this.m_compact) {
/* 118 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 119 */         this.m_sb.append("   ");
      }
    }
  }


/* 125 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 129 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 133 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }


  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 137 */     indent();

/* 139 */     this.m_sb.append('<').append(name);

/* 141 */     int len = nameValues.length;

/* 143 */     for (int i = 0; i + 1 < len; i += 2) {
/* 144 */       Object attrName = nameValues[i];
/* 145 */       Object attrValue = nameValues[i + 1];

/* 147 */       if (attrValue != null) {
/* 148 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 152 */     if (dynamicAttributes != null) {
/* 153 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 154 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }

/* 158 */     if (text != null && closed) {
/* 159 */       this.m_sb.append('>');
/* 160 */       this.m_sb.append(escape(text, true));
/* 161 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 163 */       if (closed) {
/* 164 */         this.m_sb.append('/');
      } else {
/* 166 */         this.m_level++;
      }

/* 169 */       this.m_sb.append(">\r\n");
    }
  }


  protected String toString(Object value) {
/* 175 */     if (value instanceof String)
/* 176 */       return (String)value; 
/* 177 */     if (value instanceof Collection) {
/* 178 */       Collection<Object> list = (Collection)value;
/* 179 */       StringBuilder sb = new StringBuilder(32);
/* 180 */       boolean first = true;

/* 182 */       for (Object item : list) {
/* 183 */         if (first) {
/* 184 */           first = false;
        } else {
/* 186 */           sb.append(',');
        }

/* 189 */         if (item != null) {
/* 190 */           sb.append(item);
        }
      }

/* 194 */       return sb.toString();
/* 195 */     }  if (value.getClass().isArray()) {
/* 196 */       int len = Array.getLength(value);
/* 197 */       StringBuilder sb = new StringBuilder(32);
/* 198 */       boolean first = true;

/* 200 */       for (int i = 0; i < len; i++) {
/* 201 */         Object item = Array.get(value, i);

/* 203 */         if (first) {
/* 204 */           first = false;
        } else {
/* 206 */           sb.append(',');
        }

/* 209 */         if (item != null) {
/* 210 */           sb.append(item);
        }
      }

/* 214 */       return sb.toString();
    }

/* 217 */     return String.valueOf(value);
  }

  protected String toString(Date date, String format) {
/* 221 */     if (date != null) {
/* 222 */       return (new SimpleDateFormat(format)).format(date);
    }
/* 224 */     return null;
  }




/* 230 */   public void visitDefaultServer(DefaultServer defaultServer) { startTag("default-server", true, null, new Object[] { "id", defaultServer.getId(), "weight", Double.valueOf(defaultServer.getWeight()), "port", Integer.valueOf(defaultServer.getPort()), "enable", Boolean.valueOf(defaultServer.isEnable()) }); }



  public void visitDomain(Domain domain) {
/* 235 */     startTag("domain", null, new Object[] { "id", domain.getId() });

/* 237 */     if (!domain.getGroups().isEmpty()) {
/* 238 */       for (Group group : domain.getGroups().values()) {
/* 239 */         group.accept(this.m_visitor);
      }
    }

/* 243 */     endTag("domain");
  }


  public void visitGroup(Group group) {
/* 248 */     startTag("group", null, new Object[] { "id", group.getId() });

/* 250 */     if (!group.getServers().isEmpty()) {
/* 251 */       for (Server server : group.getServers()) {
/* 252 */         server.accept(this.m_visitor);
      }
    }

/* 256 */     endTag("group");
  }



/* 261 */   public void visitGroupServer(GroupServer groupServer) { startTag("group-server", true, null, new Object[] { "id", groupServer.getId() }); }




/* 266 */   public void visitNetwork(Network network) { startTag("network", true, null, new Object[] { "id", network.getId() }); }



  public void visitNetworkPolicy(NetworkPolicy networkPolicy) {
/* 271 */     startTag("network-policy", null, new Object[] { "id", networkPolicy.getId(), "title", networkPolicy.getTitle(), "block", Boolean.valueOf(networkPolicy.isBlock()), "server-group", networkPolicy.getServerGroup() });

/* 273 */     if (!networkPolicy.getNetworks().isEmpty()) {
/* 274 */       for (Network network : networkPolicy.getNetworks().values()) {
/* 275 */         network.accept(this.m_visitor);
      }
    }

/* 279 */     endTag("network-policy");
  }


  public void visitRouterConfig(RouterConfig routerConfig) {
/* 284 */     startTag("router-config", null, new Object[] { "backup-server", routerConfig.getBackupServer(), "backup-server-port", Integer.valueOf(routerConfig.getBackupServerPort()), "startTime", toString(routerConfig.getStartTime(), "yyyy-MM-dd HH:mm:ss"), "domain", routerConfig.getDomain(), "endTime", toString(routerConfig.getEndTime(), "yyyy-MM-dd HH:mm:ss") });

/* 286 */     if (!routerConfig.getDefaultServers().isEmpty()) {
/* 287 */       for (DefaultServer defaultServer : routerConfig.getDefaultServers().values()) {
/* 288 */         defaultServer.accept(this.m_visitor);
      }
    }

/* 292 */     if (!routerConfig.getNetworkPolicies().isEmpty()) {
/* 293 */       for (NetworkPolicy networkPolicy : routerConfig.getNetworkPolicies().values()) {
/* 294 */         networkPolicy.accept(this.m_visitor);
      }
    }

/* 298 */     if (!routerConfig.getServerGroups().isEmpty()) {
/* 299 */       for (ServerGroup serverGroup : routerConfig.getServerGroups().values()) {
/* 300 */         serverGroup.accept(this.m_visitor);
      }
    }

/* 304 */     if (!routerConfig.getDomains().isEmpty()) {
/* 305 */       for (Domain domain : routerConfig.getDomains().values()) {
/* 306 */         domain.accept(this.m_visitor);
      }
    }

/* 310 */     endTag("router-config");
  }



/* 315 */   public void visitServer(Server server) { startTag("server", true, null, new Object[] { "id", server.getId(), "port", Integer.valueOf(server.getPort()), "weight", Double.valueOf(server.getWeight()) }); }



  public void visitServerGroup(ServerGroup serverGroup) {
/* 320 */     startTag("server-group", null, new Object[] { "id", serverGroup.getId(), "title", serverGroup.getTitle() });

/* 322 */     if (!serverGroup.getGroupServers().isEmpty()) {
/* 323 */       for (GroupServer groupServer : serverGroup.getGroupServers().values()) {
/* 324 */         groupServer.accept(this.m_visitor);
      }
    }

/* 328 */     endTag("server-group");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/router/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */